package com.admission.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springside.modules.utils.reflection.ConvertUtils;

/**
 * 
 * Employee.java
 * 
 * 
 * 员工信息
 */
@Entity
@Table(name = "employee")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Employee extends IdEntity implements Serializable{


	private static final long serialVersionUID = -6685916928935137890L;

	private String name;

	private String loginName;
	private String password;
	private String contact;
	private String summary;// 描述

	private boolean loginable = true; // 是否可以登录

	private Set<Role> roles = new HashSet<Role>();

	@Column(unique = true)
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isLoginable() {
		return loginable;
	}

	public void setLoginable(boolean loginable) {
		this.loginable = loginable;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@Fetch(FetchMode.SUBSELECT)
	@JoinTable(name = "employee_role", joinColumns = { @JoinColumn(name = "employee") }, inverseJoinColumns = { @JoinColumn(name = "role") })
	// 集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.NONE)
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@Transient
	public String getRoleNames() {
		return ConvertUtils.convertElementPropertyToString(roles, "name", ",");
	}

	/**
	 * 用户拥有的角色id字符串, 多个角色id用','分隔.
	 */
	// 非持久化属性.
	@Transient
	@SuppressWarnings("unchecked")
	public List<String> getRoleIds() {
		
		return ConvertUtils.convertElementPropertyToList(roles, "id");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

}
